package cfs.formbean;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.mybeans.form.FormBean;

public class CreateCustomerFormBean extends FormBean {

	private String username;
	private String password;
	private String confirmpassword;
	private String firstname;
	private String lastname;
	private String addr_line1;
	private String addr_line2;
	private String city;
	private int availablebalance;
	private String state;
	private String zip;
	private String email;

	public int getAvailablebalance() {
		return availablebalance;
	}

	public String getAddr_line1() {
		return addr_line1;
	}

	public String getAddr_line2() {
		return addr_line2;
	}

	public String getCity() {
		return city;
	}

	public String getState() {
		return state;
	}

	public String getZip() {
		return zip;
	}

	public String getEmail() {
		return email;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public String getConfirmpassword() {
		return confirmpassword;
	}

	public String getFirstname() {
		return firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setAvailablebalance(int availablebalance) {
		this.availablebalance = availablebalance;
	}

	public void setUsername(String s) {
		username = s.trim();
	}

	public void setFirstname(String s) {
		firstname = trimAndConvert(s, "<>\"");
	}

	public void setLastname(String s) {
		lastname = trimAndConvert(s, "<>\"");
	}

	public void setPassword(String s) {
		password = s.trim();
	}

	public void setConfirmpassword(String s) {
		confirmpassword = s.trim();
	}

	public void setAddr_line1(String addr_line1) {
		this.addr_line1 = this.trimAndConvert(addr_line1, "<>\"");
	}

	public void setAddr_line2(String addr_line2) {
		this.addr_line2 = this.trimAndConvert(addr_line2, "<>\"");
	}

	public void setCity(String city) {
		this.city = this.trimAndConvert(city, "<>\"");
	}

	public void setState(String state) {
		this.state = this.trimAndConvert(state, "<>\"");
	}

	public void setZip(String zip) {
		this.zip = this.trimAndConvert(zip, "<>\"");
	}

	public void setEmail(String s) {
		email = trimAndConvert(s, "<>\"");
	}

	public List<String> getValidationErrors() {
		List<String> errors = new ArrayList<String>();

		// we first iterate empty validation for all fields
		if (firstname == null || firstname.length() == 0) {
			errors.add("First name is required");
		}

		if (lastname == null || lastname.length() == 0) {
			errors.add("Last name is required");
		}

		if (username == null || username.length() == 0) {
			errors.add("User Name is required");
		}

		if (password == null || password.length() == 0) {
			errors.add("Password is required");
		}

		if (confirmpassword == null || confirmpassword.length() == 0) {
			errors.add("Confirm Password is required");
		}

		if ((addr_line1 == null || addr_line1.length() == 0)
				&& (addr_line2 == null || addr_line2.length() == 0)) {
			errors.add("addresss is required");
		}

		if (city == null || city.length() == 0) {
			errors.add("City is required");
		}

		if (state == null || state.length() == 0) {
			errors.add("state is required");
		}
		if (zip == null || zip.length() == 0) {
			errors.add("zip code is required");
		}

		if (email == null || email.length() == 0) {
			errors.add("email is required");
		}

		// the end of empty validation
		if (errors.size() > 0)
			return errors;

		if (!username.matches("[\\w]+")){
			errors.add("user name can only contain word characters");
			username = trimAndConvert(username, "<>\"");
		}

		if (password.matches(".*[<>\"].*"))
			errors.add("password  may not contain angle brackets or quotes");

		if (confirmpassword.matches(".*[<>\"].*")) {
			errors.add("password  may not contain angle brackets or quotes");
		}
		
		// the end of empty validation
		if (errors.size() > 0)
			return errors;

		// further validation for field format and boundary
		if (username.length() < 6) {
			errors.add("User Name should not less than 6");
		}

		if (password.length() < 6) {
			errors.add("Password must be more than 6 Letters");
		}

		if (!password.equals(confirmpassword)) {
			errors.add("password are not same");
		}

		try {
			Integer.parseInt(zip);
		} catch (NumberFormatException e) {
			errors.add("ZIP should be an integer");
		}

		if (isEmail(email) != true) {
			errors.add("email should format as xxx@xxx.xxx");
		}

		return errors;
	}

	public boolean isEmail(String email) {

		boolean isEmail = false;
		String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
		Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		Matcher m = pattern.matcher(email);
		if (m.matches()) {
			isEmail = true;
		}

		return isEmail;
	}

}
